package com.cherrypt.api.core.controller;

import com.cherrypt.api.common.constant.WebConst;
import com.cherrypt.api.common.enums.ResultEnum;
import com.cherrypt.api.common.exception.TipException;
import com.cherrypt.api.core.service.WaitService;
import com.cherrypt.api.core.util.JwtUtils;
import com.cherrypt.api.model.dto.WaitConfigDto;
import com.cherrypt.api.model.dto.WaitDto;
import com.cherrypt.api.model.vo.WaitVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "获取候选配置、同意、反对候选、查询自己候选情况")
@RestController
@RequestMapping(value = "${api.url.wait.value}")
public class WaitController {
    private static final Logger logger = LoggerFactory.getLogger(WaitController.class);

    @Value(value = "${api.config.post.passWaitLen}")
    private int passWaitLen;

    @Value(value = "${api.config.wait.time}")
    private long waitTime;

    @Value(value = "${api.config.wait.agree}")
    private int waitAgree;

    @Autowired
    private WaitService waitService;

    @ApiOperation(value = "获取候选配置")
    @ApiImplicitParam(name = "token", value = "Header携带token辨识用户", example = WebConst.TOKEN,
            dataType = "string", paramType = "header", required = true)
    @GetMapping(value = "${api.url.wait.config}")
    public ResponseEntity<WaitConfigDto> config(@ApiIgnore HttpServletRequest request) throws TipException {
        WaitConfigDto waitConfigDto = new WaitConfigDto();
        waitConfigDto.setPassWaitLen(passWaitLen);
        waitConfigDto.setWaitAgree(waitAgree);
        waitConfigDto.setWaitTime(waitTime / WebConst.SECOND);
        return ResponseEntity.ok(waitConfigDto);
    }

    @ApiOperation(value = "增加候选")
    @ApiImplicitParam(name = "token", value = "Header携带token辨识用户", example = WebConst.TOKEN,
            dataType = "string", paramType = "header", required = true)
    @PostMapping(value = "")
    public ResponseEntity agree(@RequestBody WaitVo waitVo, @ApiIgnore HttpServletRequest request) throws TipException {
        int userId = JwtUtils.getUserIdFromRequest(request);
        boolean isWait = waitService.agree(waitVo.getPid(), userId, waitVo.getAgree());
        if (isWait) {
            return ResponseEntity.ok().build();
        }
        throw new TipException(ResultEnum.WAIT_ERROR);
    }

    @ApiOperation(value = "查询自己候选情况")
    @ApiImplicitParam(name = "token", value = "Header携带token辨识用户", example = WebConst.TOKEN,
            dataType = "string", paramType = "header", required = true)
    @PostMapping(value = "${api.url.wait.user}")
    public ResponseEntity<WaitDto> user(@RequestBody WaitVo waitVo,
                                        @ApiIgnore HttpServletRequest request) throws TipException {
        int userId = JwtUtils.getUserIdFromRequest(request);
        if (waitVo.getPids() != null && waitVo.getPids().length > 0) {
            return ResponseEntity.ok(waitService.getWaitList(waitVo.getPids(), userId));
        } else {
            throw new TipException(ResultEnum.ARGS_ERROR);
        }
    }
}
